package com.to.aboomy.bannersample.slice;

import com.to.aboomy.banner.Banner;
import com.to.aboomy.banner.HolderCreator;
import com.to.aboomy.banner.ImageHolderCreator;
import com.to.aboomy.banner.IndicatorView;
import com.to.aboomy.bannersample.pagetransformer.ScalePageTransformer;
import com.to.aboomy.bannersample.ResourceTable;
import com.to.aboomy.bannersample.util.ToastUtil;
import com.to.aboomy.bannersample.util.Utils;
import com.to.aboomy.bannersample.viewpager.creator.ImageHolderCreator2;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.PageSlider;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.Resource;
import ohos.global.resource.ResourceManager;
import ohos.global.resource.WrongTypeException;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;

import java.io.IOException;

import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_PARENT;

/**
 * @since 2021-07-26
 */
public class
MultBannerAbilitySlice extends AbilitySlice implements HolderCreator {
    private Banner banner;
    private Banner banner2;
    private Banner banner3;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_multbanner);
        initBanner1();
        initBanner2();
        initBanner3();
    }

    private void initBanner1() {
         banner = (Banner) findComponentById(ResourceTable.Id_banner1);
        banner.setHolderCreator(this).setOuterPageChangeListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int i, float v, int i1) {

            }

            @Override
            public void onPageSlideStateChanged(int i) {
            }

            @Override
            public void onPageChosen(int i) {

            }
        }).setOnPageClickListener(position -> ToastUtil.showToast(getAbility(),"" + position)).setHolderCreator(new ImageHolderCreator2()).setIndicator(new IndicatorView(this).setIndicatorColor(Color.GRAY).setIndicatorSelectorColor(Color.WHITE) .setIndicatorStyle(IndicatorView.INDICATOR_CIRCLE))
        .setPageMargin(-(AttrHelper.vp2px(110,getContext())),AttrHelper.vp2px(60,getContext())).setCornerRadius(20).setPages(Utils.getImage(2));
    }
    private void initBanner2() {
         banner2 = (Banner) findComponentById(ResourceTable.Id_banner2);
        banner2.setHolderCreator(this)
                .setAutoTurningTime(3000)
                .setIndicator(((IndicatorView) findComponentById(ResourceTable.Id_indicator2))
                        .setIndicatorColor(Color.GRAY)
                        .setIndicatorStyle(IndicatorView.INDICATOR_BEZIER)
                        .setIndicatorSelectorColor(Color.RED), false)
                .setPageMargin(-(AttrHelper.vp2px(88,getContext())),AttrHelper.vp2px(37,getContext()))
                .setPageTransformer(true, new ScalePageTransformer())
                .setOnPageClickListener(position -> ToastUtil.showToast(getAbility(),"" + position))
                .setCornerRadius(20)
                .setPages(Utils.getImage(3));
        banner2.setAutoPlay(true);
    }
    private void initBanner3() {
         banner3 = (Banner) findComponentById(ResourceTable.Id_banner3);
        banner3.setIndicator(new IndicatorView(this)
                .setIndicatorColor(Color.GRAY)
                .setIndicatorSelectorColor(Color.WHITE)
                .setIndicatorStyle(IndicatorView.INDICATOR_CIRCLE_RECT));
        banner3.setAutoTurningTime(3500);
        banner3.setHolderCreator(new ImageHolderCreator());
        banner3.setCornerRadius(20);
        banner3.setOnPageClickListener(position -> ToastUtil.showToast(getAbility(),"" + position));

        banner3.setPageMargin(-370,100);
        banner3.setPageTransformer(true, new ScalePageTransformer());
        banner3.setPages(Utils.getImage(4));
    }

    @Override
    public Component createView(Context context, int integer,int index) {
        Image image = new Image(context);;
        image.setScaleMode(Image.ScaleMode.STRETCH);
        image.setLayoutConfig(new ComponentContainer.LayoutConfig(MATCH_PARENT, MATCH_PARENT));
        try {
            ImageSource.SourceOptions options = new ImageSource.SourceOptions();
            options.formatHint = "image/jpg";
            ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
            ResourceManager manager = context.getResourceManager() ;
            String path = manager.getMediaPath( integer);
            Resource r1 = manager.getRawFileEntry(path).openRawFile();
            ImageSource source = ImageSource.create(r1,options);
            PixelMap p1 = source.createPixelmap(decodingOptions);
            image.setPixelMap(p1);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotExistException e) {
            e.printStackTrace();
        } catch (WrongTypeException e) {
            e.printStackTrace();
        }
        return image;
    }

}
